
package com.prj.ufdm.cloud.zipken.api;

import java.util.Random;
import java.util.concurrent.Callable;

import brave.Span;
import brave.Tracer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.prj.ufdm.cloud.zipken.config.SampleBackground;

@RestController
@RequestMapping("/api/zipkin")
public class SleuthZipkinSyntheticApi implements ApplicationListener<ServletWebServerInitializedEvent> {

	private static final Log log = LogFactory.getLog(SleuthZipkinSyntheticApi.class);

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private Tracer tracer;

	@Autowired
	private SampleBackground controller;

	private Random random = new Random();

	private int port;

	/**
	 * DESC: 调用本地设置Tag（/api/zipkin/settag）服务
	 * @auther 胡义振
	 * @date 2020年2月22日
	 * @return
	 * @throws InterruptedException 
	 * @return
	 */
	@RequestMapping("/tag")
	public String doTestTag() throws InterruptedException {
		Thread.sleep(this.random.nextInt(1000));
		log.info("tag");
		String s = this.restTemplate.getForObject("http://localhost:" + this.port + "/api/zipkin/settag", String.class);
		return "tag/" + s;
	}
	
	/**
	 * DESC: 通过 tracer 设置 tag
	 * @auther 胡义振
	 * @date 2020年2月22日
	 * @return
	 * @throws InterruptedException 
	 * @return
	 */
	@RequestMapping("/settag")
	public String doSetTag() throws InterruptedException {
		log.info("settag");
		int millis = this.random.nextInt(1000);
		Thread.sleep(millis);
		this.tracer.currentSpan().tag("random-sleep-millis", String.valueOf(millis));
		return "settag";
	}

	
	@RequestMapping("/call")
	public Callable<String> call() {
		return new Callable<String>() {
			@Override
			public String call() throws Exception {
				int millis = SleuthZipkinSyntheticApi.this.random.nextInt(1000);
				Thread.sleep(millis);
				Span currentSpan = SleuthZipkinSyntheticApi.this.tracer.currentSpan();
				currentSpan.tag("callable-sleep-millis", String.valueOf(millis));
				return "async hi: " + currentSpan;
			}
		};
	}

	@RequestMapping("/async")
	public String async() throws InterruptedException {
		log.info("async");
		this.controller.background();
		return "ho";
	}



	@RequestMapping("/traced")
	public String traced() throws InterruptedException {
		Span span = this.tracer.nextSpan().name("http:customTraceEndpoint").start();
		int millis = this.random.nextInt(1000);
		log.info(String.format("Sleeping for [%d] millis", millis));
		Thread.sleep(millis);
		this.tracer.currentSpan().tag("random-sleep-millis", String.valueOf(millis));

		String s = this.restTemplate.getForObject("http://localhost:" + this.port + "/call", String.class);
		span.finish();
		return "traced/" + s;
	}

	@RequestMapping("/start")
	public String start() throws InterruptedException {
		int millis = this.random.nextInt(1000);
		log.info(String.format("Sleeping for [%d] millis", millis));
		Thread.sleep(millis);
		this.tracer.currentSpan().tag("random-sleep-millis", String.valueOf(millis));
		String s = this.restTemplate.getForObject("http://localhost:" + this.port + "/call", String.class);
		return "start/" + s;
	}

	@Override
	public void onApplicationEvent(ServletWebServerInitializedEvent event) {
		this.port = event.getSource().getPort();
	}

}
